//
//  TableViewC_TuiJianPinDao.m
//  HappyStore
//
//  Created by 陈锋 on 16/5/25.
//  Copyright © 2016年 LLJ. All rights reserved.
//

#import "TableViewC_TuiJianPinDao.h"


@interface TableViewC_TuiJianPinDao ()<UIAlertViewDelegate>{
    NSString *urlStr;
}

@end

@implementation TableViewC_TuiJianPinDao


- (void)viewDidLoad {
    [super viewDidLoad];
    
    _aryInfo = [NSArray array];
    
    _index = 1;
}

- (void)setDelegte
{
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return  _aryInfo.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellLoveSingle = @"cellLoveSingle";//首页
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellLoveSingle forIndexPath:indexPath];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.accessoryType = UITableViewCellAccessoryNone;
    
    
    _titleLabel = [cell viewWithTag:21];
    _titleLabel.text = _aryInfo[indexPath.row][@"userName"];
    
    CGSize size = [_titleLabel sizeThatFits:CGSizeMake(MAXFLOAT, 21)];
    if (size.width > 160) {
        _titleLabel.width = 160;
    }else{
        _titleLabel.width = size.width;
    }
    
    
    //_goveImg = [cell viewWithTag:23];
    //_goveImg.frame = CGRectMake(_titleLabel.width + _titleLabel.x + 5, _titleLabel.y + 5, 50, 15);

    _logoImg = [cell viewWithTag:24];
    //会员图标
    _logoImg.image = [UsedTool setVipimage:_aryInfo[indexPath.row][@"vipLevel"]];
//    _logoImg.frame = CGRectMake(_titleLabel.width + _titleLabel.x + 5, _titleLabel.y + 7, 10, 10);
    
    _addressLabel = [cell viewWithTag:22];
    _addressLabel.text = _aryInfo[indexPath.row][@"address"];

    
    _btn = [cell viewWithTag:25];
    
    UIButton *btn = [[UIButton alloc]init];
    btn.size = _btn.size;
    btn.tag = indexPath.row;

    [btn addTarget:self action:@selector(goGove:) forControlEvents:UIControlEventTouchUpInside];
    
    if ([_aryInfo[indexPath.row][@"isFoucs"] isEqualToString:@"是"]) {
        
        btn.selected = YES;
        _btn.selected = YES;
        
    }else{
        
        btn.selected = NO;
        _btn.selected = NO;
        
    }
    [_btn addSubview:btn];
//    NSLog(@"----------------%@",btn);
    
    _headImg = [cell viewWithTag:20];
    [_headImg.layer setCornerRadius:(_headImg.frame.size.height/2)];
    [_headImg.layer setMasksToBounds:YES];
    [_headImg setContentMode:UIViewContentModeScaleAspectFill];
    [_headImg setClipsToBounds:YES];
    _headImg.layer.shadowColor = [UIColor blackColor].CGColor;
    _headImg.layer.shadowOffset = CGSizeMake(4, 4);
    _headImg.layer.shadowOpacity = 0.5;
    _headImg.layer.shadowRadius = 2.0;
    _headImg.userInteractionEnabled = YES;
//    [headImg sd_setImageWithURL:_dataDict[@"headPhoto"] placeholderImage:[UIImage imageNamed:@"20100403114758394_4147"]];
    _headImg.image = [UIImage imageNamed:@"20100403114758394_4147"];
    return cell;
}

-(void)goGove:(UIButton *)button{

    if (allConstMemberid) {
        
        button.selected = !button.selected;
        
        NSMutableDictionary * dic = [NSMutableDictionary dictionary];
        dic[@"userId"] = allConstMemberid;
        dic[@"token"] = allConstMemberToken;
        dic[@"targetUuserIid"] = _aryInfo[button.tag][@"userId"];
        
        if (button.selected == NO) {
            
            urlStr = @"focusUser_cannel_app_myHome.im.lensum";
            
        }else if (button.selected == YES){
            
            urlStr = @"focusUser_app_myHome.im.lensum";
            
        }
        
        [LSHttpTool sendHttpData:urlStr params:dic success:^(id responseObject) {
//            NSDictionary *dicInfo = [NSDictionary dictionaryWithDictionary:responseObject];
//            if ([dicInfo[@"result"] integerValue] == 1) {
////                if (dicInfo[@"data"][]) {
////                    <#statements#>
////                }
//                [button setImage:[UIImage imageNamed:@"首页-已关注"] forState:UIControlStateNormal];
//                
//            }else{

//            }
            [self sendRequest];
        } failure:^(NSError *error) {

            [self showSingleIconBubble:@"网络连接错误"];
            
        }];

    }else{
        
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"提示" message:@"请先登录" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"登录", nil];
        [alert show];

    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];//取消选中状态
    goveDetailVC * goveDetail = [[goveDetailVC alloc]init];
    goveDetail.userType = @"3";
    goveDetail.userId = _aryInfo[indexPath.row][@"userId"];
    goveDetail.hidesBottomBarWhenPushed = YES;
    [_homeVC.navigationController pushViewController:goveDetail animated:YES];
    
    
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 1) {
        
        //登录
        UIStoryboard *LoginAndRegister = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        UINavigationController *loginNAV = [LoginAndRegister instantiateViewControllerWithIdentifier:@"LoginNav"];
        [self presentViewController:loginNAV animated:YES completion:nil];
        
        
        [self viewWillAppear:YES];
    }
}

-(void)sendRequest{
    NSMutableDictionary * dic = [NSMutableDictionary dictionary];

    dic[@"page"] = @(_index);
    dic[@"userId"] = allConstMemberid;
    
    [LSHttpTool sendHttpData:@"homeTuijianGove_app_user.im" params:dic success:^(id responseObject) {
        NSDictionary *dicInfo = [NSDictionary dictionaryWithDictionary:responseObject];
        
        _aryInfo = dicInfo[@"data"][@"tuijianGove"];
        
        if ([urlStr isEqualToString:@"focusUser_cannel_app_myHome.im.lensum"]) {

            [self showRightWithTitle:@"取消关注!" autoCloseTime:2];
            
        }else if ([urlStr isEqualToString:@"focusUser_app_myHome.im.lensum"]){
            
            [self showRightWithTitle:@"关注成功!" autoCloseTime:2];
        }

        
        [self.tableView reloadData];

        
    } failure:^(NSError *error) {

        [self showSingleIconBubble:@"网络连接错误"];
        
    }];

}
@end
